﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Masters.ModuleData
{
    #region DRMasters

    public class DRMasters : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private SqlABParameter sqlP2;
        private string type;
        private string code;

        public DRMasters(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.Type);
            sqlP2 = new SqlABParameter("@p51", PL_Masters.Code);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.Type, sqlP1);
            qB.AddCompare(PL_Masters.Code, sqlP2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = type;
            sqlP2.Value = code;
        }

        public bool Find(string type, string code)
        {
            this.type = type;
            this.code = code;
            return base.Find();
        }
    }

    #endregion

    #region DRMastersByTaxNo

    public class DRMastersByTaxNo : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string taxNo;

        public DRMastersByTaxNo(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.TaxNo);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.TaxNo, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = taxNo;
        }

        public bool Find(string taxNo)
        {
            this.taxNo = taxNo;
            return base.Find();
        }
    }

    #endregion

    #region DRMastersByFiscalCode

    public class DRMastersByFiscalCode : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string fiscalcode;

        public DRMastersByFiscalCode(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.FiscalCode);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.FiscalCode, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = fiscalcode;
        }

        public bool Find(string fiscalCode)
        {
            this.fiscalcode = fiscalCode;
            return base.Find();
        }
    }

    #endregion

    #region RRMasterByMasterType

    public class RRMasterByMasterType : DataReaderUpdater
    {
        public string MasterType { get; set; }

        private SqlABParameter sqlP1;

        public RRMasterByMasterType()
            : base(false)
        {
            MasterType = "";
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.Type);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.Type, sqlP1);
            qB.AddOrder(PL_Masters.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);

            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = MasterType;
        }
    }

    #endregion

    #region DRMasterType

    public class DRMasterType : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string code;

        public enum Module { All, Master, Accounting };

        private Module module;

        public DRMasterType(Module module)
            : base(false)
        {
            this.module = module;
        }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_MasterType.Code);

            qB.AddSelect();
            qB.AddFrom(PL_MasterType.Name);
            qB.AddCompare(PL_MasterType.Code, sqlP1);
            qB.AddOrder(PL_MasterType.IsPrefer, true);
            qB.AddOrder(PL_MasterType.Code);
            switch (module)
            {
                case Module.Accounting:
                    qB.AddCompare<bool>(PL_MasterType.IsAccountingType, true);
                    break;

                case Module.Master:
                    qB.AddCompare<bool>(PL_MasterType.IsAccountingType, false);
                    break;
            }

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = code;
        }
    }

    #endregion

    #region DRMasterTypeByType

    public class DRMasterTypeByType : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private MasterTypes type;

        public DRMasterTypeByType()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_MasterType.Code);

            qB.AddSelect();
            qB.AddFrom(PL_MasterType.Name);
            qB.AddCompare(PL_MasterType.MasterType, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(MasterTypes type)
        {
            this.type = type;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = (int)type;
        }
    }

    #endregion

    #region RRMasterTypeByType

    public class RRMasterTypeByType : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private MasterTypes type;

        public RRMasterTypeByType(MasterTypes masterType)
            : base(false)
        {
            type = masterType;
        }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_MasterType.MasterType);
            sqlP1.Value = (int)type;

            qB.AddSelect();
            qB.AddFrom(PL_MasterType.Name);
            qB.AddCompare(PL_MasterType.MasterType, sqlP1);
            qB.AddOrder(PL_MasterType.IsPrefer, true);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeForInvoice

    public class RRMasterTypeForInvoice : DataReaderUpdater
    {
        public RRMasterTypeForInvoice()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<bool>(PL_MasterType.CanReceiveInvoice, true);
            qb.AddCompare<bool>(PL_MasterType.IsAccountingType, false);
            qb.AddOrder(PL_MasterType.IsPrefer, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeForEmittedDocNo

    public class RRMasterTypeForEmittedDocNo : DataReaderUpdater
    {
        public RRMasterTypeForEmittedDocNo()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AND_OR = "OR";
            qb.AddCompare<AccountType>(PL_MasterType.AccountType, AccountType.E_Liabilities);
            qb.AddCompare<AccountType>(PL_MasterType.AccountType, AccountType.E_Costs);
            qb.AddOrder(PL_MasterType.IsPrefer, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeForMaster

    public class RRMasterTypeForMaster : DataReaderUpdater
    {
        public RRMasterTypeForMaster()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_PurchaseInvoice, "!=");
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_SaleInvoice, "!=");
            qb.AddCompare<CodeMode>(PL_MasterType.CodeMode, CodeMode.E_IsEmpty, "!=");
            qb.AddCompare<bool>(PL_MasterType.IsAccountingType, false);
            qb.AddOrder(PL_MasterType.IsPrefer, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeForAccountingMaster

    public class RRMasterTypeForAccountingMaster : DataReaderUpdater
    {
        public RRMasterTypeForAccountingMaster()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_PurchaseInvoice, "!=");
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_SaleInvoice, "!=");
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_Tax, "!=");
            qb.AddCompare<CodeMode>(PL_MasterType.CodeMode, CodeMode.E_IsEmpty, "!=");
            qb.AddCompare<bool>(PL_MasterType.IsAccountingType, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    public class RRMasterTypeForTax : DataReaderUpdater
    {
        public RRMasterTypeForTax()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_Tax);
            qb.AddCompare<bool>(PL_MasterType.IsAccountingType, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterType

    public class RRMasterType : DataReaderUpdater
    {
        public RRMasterType()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeNoAdmin

    public class RRMasterTypeNoAdmin : DataReaderUpdater
    {
        public RRMasterTypeNoAdmin()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_Administrator, "!=");
            qb.AddOrder(PL_MasterType.Description);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region RRMasterTypeForMaster

    public class RRMasterTypeForJEPurchase : DataReaderUpdater
    {
        public RRMasterTypeForJEPurchase()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AND_OR = "OR";
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_Other);
            qb.AddCompare<MasterTypes>(PL_MasterType.MasterType, MasterTypes.E_Condominium);
            qb.AddOrder(PL_MasterType.IsPrefer, true);
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region DRTitle

    public class DRTitle : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string title;

        public DRTitle(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Titles.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_Titles.Title);

            qB.AddSelect();
            qB.AddFrom(PL_Titles.Name);
            qB.AddCompare(PL_Titles.Title, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string title)
        {
            this.title = title;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = title;
        }
    }

    #endregion

    #region DRTaxCode

    public class DRTaxCode : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string taxCode;

        public DRTaxCode(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_TaxCode.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_TaxCode.Code);

            qB.AddSelect();
            qB.AddFrom(PL_TaxCode.Name);
            qB.AddCompare(PL_TaxCode.Code, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string taxCode)
        {
            this.taxCode = taxCode;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = taxCode;
        }
    }

    #endregion

    #region RRTaxCode

    public class RRTaxCode : DataReaderUpdater
    {
        public RRTaxCode()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_TaxCode.Name;

            QueryBuilder qB = new QueryBuilder();
            qB.SetQualified = true;

            qB.AddSelect();
            qB.AddFrom(PL_TaxCode.Name);
            qB.AddOrder(PL_TaxCode.Percent);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }

    #endregion

    #region DRDescriptions

    public class DRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private DescriptionType descType;
        private string code;

        public DRDescriptions(DescriptionType descType)
            : this(descType, false)
        { }

        public DRDescriptions(DescriptionType descType, bool updater)
            : base(updater)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = PL_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PL_Descriptions.Type);
            p2 = new SqlABParameter("@p51", PL_Descriptions.Code);

            qB.AddSelect();
            qB.AddFrom(PL_Descriptions.Name);
            qB.AddCompare(PL_Descriptions.Type, p1);
            qB.AddCompare(PL_Descriptions.Code, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
            p2.Value = code;
        }
    }

    #endregion

    #region RRDescriptions

    public class RRDescriptions : DataReaderUpdater
    {
        private SqlABParameter p1;
        private DescriptionType descType;

        public RRDescriptions(DescriptionType descType)
            : base(false)
        {
            this.descType = descType;
        }

        protected override void CreateConnection()
        {
            myTable = PL_Descriptions.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PL_Descriptions.Type);

            qB.AddSelect();
            qB.AddFrom(PL_Descriptions.Name);
            qB.AddCompare(PL_Descriptions.Type, p1);
            qB.AddOrder(PL_Descriptions.Code);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = descType.Int();
        }
    }

    #endregion

    public class DRCountCompany : DataReaderUpdater
    {
        private SqlABParameter p1;

        public DRCountCompany()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Company.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", PL_Descriptions.Type);

            qB.AddSelect();
            qB.AddFrom(PL_Company.Name);
            qB.AddCompare(PL_Company.Code, p1);
            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            p1.Value = 1;
        }
    }

}